<!-- 登录界面 -->
<template>
  <div class="login">
    <div class="box">
      <div class="logo">
        <img src="../assets/logo.png" alt="logo" />
      </div>
      <!-- 登录表格 -->
      <el-form
        class="form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginRef"
      >
        <!-- 用户 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
            placeholder="用户名"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-warning"
            v-model="loginForm.password"
            show-password
            placeholder="请输入密码"
          />
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginRef.resetFields()
    },
    login() {
      this.$refs.loginRef.validate(async vali => {
        // 检测初步认证
        if (!vali) return
        //检测是否为数据库中的
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status != 200) return this.$message.error('登录失败~')
        // 登录成功后
        this.$message.success('欢迎回来！')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/Home')
      })
    }
  }
}
</script>

<style lang="less">
.login {
  width: 100%;
  height: 100vh;
  background-color: rgb(38, 74, 107);
  position: relative;
}
.box {
  width: 500px;
  height: 300px;
  background-color: #fff;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .logo {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    padding: 6px;

    border: 1px solid #cccccc;
    background-color: #fff;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);

    img {
      border-radius: 50%;
      width: 80px;
      height: 80px;
      background-color: rgb(236, 238, 235);
    }
  }
}
.form {
  position: absolute;
  box-sizing: border-box;
  width: 100%;
  bottom: 30px;
  padding: 0 20px;
}
.btns {
  float: right;
}
</style>
